package com.zrf.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.session.factory.HibernateSessionFactory;
import com.zrf.pojo.Detail;
import com.zrf.pojo.Refund;
import com.zrf.pojo.User;

@Path("business")
public class BusinessService {

	@GET
	@Path("pay/{ubankcard},{utradingpassword},{mbankcard},{pay}")
	@Produces(MediaType.APPLICATION_JSON)
	public String pay(@PathParam("ubankcard") String ubc,@PathParam("utradingpassword") String utp,
			@PathParam("mbankcard") String mbk,@PathParam("pay") Double pay){
		int state = 1;
		Session session = HibernateSessionFactory.getSession();
		Transaction t = session.getTransaction();
		try {
			
			String hql = "from User u where u.ubankcard=?";
			Query query = session.createQuery(hql);
			query.setString(0, ubc);
			User u = (User) query.uniqueResult();
			String hql2 = "from User m where m.ubankcard=?";
			Query query2 = session.createQuery(hql2);
			query2.setString(0, mbk);
			User m = (User) query2.uniqueResult();
			t.begin();
			
			if(!(utp.equals(u.getUtradingpassword()))){
				state = 0;
				return "{\"pay\":[{\"state\":\""+state+"\"}]}";
			}
			if(pay>u.getUbalance()){
				state = 0;
				return "{\"pay\":[{\"state\":\""+state+"\"}]}";
			}
			u.setUbalance(u.getUbalance()-pay);
			session.save(u);
			m.setUbalance(m.getUbalance()+pay);
			session.save(m);
			Detail d = new Detail();
			d.setIncome(0d);
			d.setPay(pay);
			d.setUserByUid(u);
			d.setUserByMid(m);
			session.save(d);
			t.commit();
			HibernateSessionFactory.closeSession();
			return "{\"pay\":[{\"state\":\""+state+"\"}]}";
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return "{\"pay\":[{\"state\":\"0\"}]}";
		}

	}
	
	
	
	
	@GET
	@Path("refund/{ubankcard},{mbankcard},{refundmoney},{state}")
	@Produces(MediaType.APPLICATION_JSON)
	public String refund(@PathParam("ubankcard") String ubk,@PathParam("mbankcard") String mbk,
			@PathParam("refundmoney") Double rm,@PathParam("state") int state){
		
		int s = 1;
		Session session = HibernateSessionFactory.getSession();
		Transaction t =session.getTransaction();
		t.begin();
		try {
			String hql1 = "from User u where u.ubankcard=?";
			Query query1 = session.createQuery(hql1);
			query1.setString(0, ubk);
			User u = (User) query1.uniqueResult();
			String hql2 = "from User m where m.ubankcard=?";
			Query query2 = session.createQuery(hql2);
			query2.setString(0, mbk);
			User m = (User) query2.uniqueResult();
			//不同意退款
			if(state==0){
				Refund r = new Refund();
				r.setUserByUid(u);
				r.setUserByMid(m);
				r.setRefundmoney(rm);
				r.setState("不同意退款");
				session.save(r);
				t.commit();
				s = 0;
				return "{\"refund\":[{\"state\":\""+s+"\"}]}";
			}
			if(state==1){
				u.setUbalance(u.getUbalance()+rm);
				session.save(u);
				m.setUbalance(m.getUbalance()-rm);
				session.save(m);
				Refund r = new Refund();
				r.setUserByUid(u);
				r.setUserByMid(m);
				r.setRefundmoney(rm);
				r.setState("同意退款,已退款");
				session.save(r);
				t.commit();
				return "{\"refund\":[{\"state\":\""+s+"\"}]}";
			}
			
			HibernateSessionFactory.closeSession();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return "{\"refund\":[{\"state\":\"0\"}]}";
	}
	
	
	
	@GET
	@Path("check/{ubankcard},{utradingpassword}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Detail> check(@PathParam("ubankcard") String ubc,@PathParam("utradingpassword") String utp){
		List<Detail> list = new ArrayList<Detail>();
		Session session = HibernateSessionFactory.getSession();
		String hql = "from User u where u.ubankcard=? and u.utradingpassword=?";
		try {
			Query query = session.createQuery(hql);
			query.setString(0, ubc);
			query.setString(1, utp);
			User u = (User) query.uniqueResult();
			String hql2 = "from Detail d where d.uid=?";
			Query query2 = session.createQuery(hql2);
			query.setInteger(0, u.getUid());
			list = query.list();
			HibernateSessionFactory.closeSession();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		return list;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
